package com.tms.service.impl;

import com.tms.domain.Status;
import com.tms.mapper.develop.LifeCycleStatusMapper;
import com.tms.service.LifeCycleStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

/**
 * 业务实现
 *
 * @author ozj
 */
@Service
public class LifeCycleStatusServiceImpl implements LifeCycleStatusService {

    @Autowired
    private LifeCycleStatusMapper lifeCycleStatusMapper;

    /**
     * 根据ID查询
     *
     * @param id 欲查询的status的id
     * @return 查询到的status
     * @author ozj
     */
    @Override
    @Cacheable(cacheNames = "lifeCycleStatus", key = "#id", unless = "#result == null")
    public Status findById(int id) {
        return lifeCycleStatusMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过name查询
     *
     * @param name 欲查询的status的名字
     * @return 查询到的status
     * @author ozj
     */
    @Override
    @Cacheable(cacheNames = "lifeCycleStatus", key = "#name", unless = "#result == null")
    public Status findByName(String name) {
        return lifeCycleStatusMapper.selectByName(name);
    }

    /**
     * 创建
     *
     * @param status 欲创建的状态
     * @return 创建的Status，其中id值为分配的id
     * @author ozj
     */
    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "lifeCycleStatus", key = "#result.id", unless = "#result == null || #result.id == 0"),
                    @CachePut(cacheNames = "lifeCycleStatus", key = "#result.name", unless = "#result == null || #result.id == 0")
            }
    )
    public Status create(Status status) {
        lifeCycleStatusMapper.insertSelective(status);
        return status;
    }

    /**
     * 根据ID删除
     *
     * @param status 欲删除的status的id
     * @author ozj
     */
    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "lifeCycleStatus", key = "#status.id", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "lifeCycleStatus", key = "#status.name", allEntries = true, beforeInvocation = true)
            }
    )
    public void deleteById(Status status) {
        lifeCycleStatusMapper.deleteByPrimaryKey(status.getId());
    }

    /**
     * 根据ID更新
     *
     * @param status 欲更新的status
     * @author ozj
     */
    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "lifeCycleStatus", key = "#status.id", unless = "#result == null"),
                    @CachePut(cacheNames = "lifeCycleStatus", key = "#status.name", unless = "#result == null")
            }
    )
    public Status updateById(Status status) {
        lifeCycleStatusMapper.updateByPrimaryKeySelective(status);
        return status;
    }
}